//
//  ExhibitionViewController.m
//  HAODE
//
//  Created by LiYinDi on 16/4/8.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "ExhibitionViewController.h"
//
#import "ExhibitionCategoryViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "ExhibitionObject.h"
#import "EGORefreshTableHeaderView.h"
#import "ToBindCompanyViewController.h"
//@protocol TestJSObjectProtocol <JSExport>

// @end
@interface ExhibitionViewController ()<UIWebViewDelegate,TestDelegate,EGORefreshTableHeaderDelegate,UIScrollViewDelegate>
{
    //下拉视图
    EGORefreshTableHeaderView * _refreshHeaderView;
    //刷新标识，是否正在刷新过程中
    BOOL _reloading;

}
@end

@implementation ExhibitionViewController
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"橱窗";
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [self setNavigation];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeToken) name:@"changeTokenId" object:Nil];
    
}
//    [self setNavigation];
////     [self userStatus];
////    [self performSelector:@selector(userStatus) withObject:nil afterDelay:3.0f];
//
- (void)viewDidLoad {
    [super viewDidLoad];
    [self ChangeToken];

}
-(void)ChangeToken{

        [self setAutolayout];
        [self userStatus];
        self.exhibitionWebview.scrollView.delegate = self;
        [self loadPage];
        
        //初始化refreshView，添加到webview 的 scrollView子视图中
        if (_refreshHeaderView == nil) {
            _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0-self.exhibitionWebview.scrollView.bounds.size.height, self.exhibitionWebview.scrollView.frame.size.width, self.exhibitionWebview.scrollView.bounds.size.height)];
            
            _refreshHeaderView.delegate = self;
            [self.exhibitionWebview.scrollView addSubview:_refreshHeaderView];
        }
        [_refreshHeaderView refreshLastUpdatedDate];
    
    
       [[self rdv_tabBarController] setTabBarHidden:NO];
    }

-(void)userStatus{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"userStatus"] isEqualToString:@"E"]) {
        //        [self showAlertMessage:@"未认证！需要认证门店才能用！"];
        //         NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
        ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
        Login.viewSat=@"The original road";
        [self.navigationController pushViewController:Login animated:YES];
        
        
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag==0) {
        if (buttonIndex == 0) {
            
            ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
            Login.viewSat=@"The original road";
            [self.navigationController pushViewController:Login animated:YES];
            
            
        }
        
}}


- (void)loadPage {
    [self.exhibitionWebview setBackgroundColor:[UIColor clearColor]];
    NSInteger numtime = [NSDate date].timeIntervalSince1970;
    NSString *numtimestr = [NSString stringWithFormat:@"%ld",numtime];
    NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
       NSLog(@"str1111===%@",str1);
    NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1
                                                           ofType:@"plist"];
    NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
      NSLog(@"dictionary1===%@",dictionary1);
    NSString *urlHead = [dictionary1 objectForKey:@"ExhibitionUrlHead"];
    NSString *urlStr=[NSString stringWithFormat:@"%@/standard/exhibition_type_list.html?v=%@",urlHead,numtimestr];
    NSURL *url = [[NSURL alloc]initWithString:urlStr];
    [self.exhibitionWebview loadRequest:[NSURLRequest requestWithURL:url]];
    NSLog(@"------%@",urlStr);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//   [self performSelector:@selector(getAchieveWebViewHeight) withObject:nil afterDelay:0.3];
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.exhibitionWebview.scrollView];
    
    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    //js调用iOS
    //第一种情况
    //其中test1就是js的方法名称，赋给是一个block 里面是iOS代码
    
    ExhibitionObject *exhiObject=[ExhibitionObject  new];
    exhiObject.delegate=self;
    context[@"appJs"]=exhiObject;
   
   
}
-(void)CommonH5Page:(NSString *)title url:(NSString *)url shareType:(NSString *)shareType contentId:(NSString *)contentId{


    ExhibitionCategoryViewController *vc=[[ExhibitionCategoryViewController alloc] init];
    vc.url=url;
    vc.shareType=shareType;
    vc.titleName=title;
    vc.exhibitionID=contentId;
    [self.navigationController pushViewController:vc animated:YES  ];

}


//-(void)pageItem:(NSString *)url exhibitionID:(NSString *)exhibitionID
//{
//    ExhibitionCategoryViewController *vc=[[ExhibitionCategoryViewController alloc] init];
//    vc.url=url;
//    vc.exhibitionID=exhibitionID;
//    [self.navigationController pushViewController:vc animated:YES  ];
//
////    NSLog(@"url>>>>//===%@",url);
//   
//}


#pragma mark - webview delegate

- (void)webViewDidStartLoad:(UIWebView *)webView {
    _reloading = YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"load page error:%@", [error description]);
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.exhibitionWebview.scrollView];
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
     [self performSelector:@selector(userStatus) withObject:nil afterDelay:3.0f];
    [self loadPage];
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return _reloading; // should return if data source model is reloading
    
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
}

-(void)setNavigation
{
    [[self rdv_tabBarController] setTabBarHidden:self.isHidden];
 
    self.navigationController.navigationBarHidden=NO;
     [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];
    backButton.hidden=!self.isHidden;
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
   

    
    UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0,100, 21)];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=self.titleStr;
    
    self.navigationItem.titleView = titleLabel;
    
    self.navigationController.navigationBar.barStyle =UIBaselineAdjustmentNone ;
    
     UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    lineLabel.textColor=[UIColor clearColor];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
 
}
-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setAutolayout
{
//    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.view.mas_bottom);
//        make.left.equalTo(self.view.mas_left);
//        make.right.equalTo(self.view.mas_right);
//        make.top.equalTo(self.view.mas_top).with.offset(100);
//        NSLog(@"WWWWW=========%f",_label.frame.size.width);
//        
//    }];
    [_exhibitionWebview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).with.offset(64) ;
//        NSLog(@"WWWWW=========%f",_exhibitionWebview.frame.size.width);
        
    }];
   
    
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end
